String.prototype.trim = function() {
    var str = this,
        str = str.replace(/^\s\s*/, ''),
        ws = /\s/,
        i = str.length;
    while (ws.test(str.charAt(--i)));
    return str.slice(0, i + 1);
}
imgVer({
    el:'$("#imgVer")',
    width:'260',
    height:'116',
    img:[
        'images/ver.png',
        'static/images/login/ver-1.png',
        'static/images/login/ver-2.png',
        'static/images/login/ver-3.png'
    ],
    success:function () {
        $.ajaxSettings.async = false;
        $.post("/login",
            {username:$("#username").val(),password:$("#password").val(),rememberme:$("#rememberme").prop("checked")},
            function (result) {
            if(result.success){
                window.location.href = "/main";
            }else{
                $(".login").css({
                    "left":"0",
                    "opacity":"1"
                });
                $(".verBox").css({
                    "left":"404px",
                    "opacity":"0"
                });
                $("#username").val("");
                $("#password").val("");
                $(".tips").html(result.msg);
                $("#logo").css({"width":"72px"});
                $("#logo").attr("src",'static/images/login/login-err.png');
            }
        });
        $.ajaxSettings.async = true;
    },
    error:function () {
        //alert('错误什么都不执行')
    }
});
$(function () {
    $(document.documentElement).on("keydown", function (e) {
        if(e.keyCode==13){
            if($(".login").css("opacity")=="1"){
                $("#submit1").click();
            }else if($(".regis").css("opacity")=="1"){
                $("#submit2").click();
            }else if($(".msglogin").css("opacity")=="1"){
                $("#submit3").click();
            }
        }
    });
});
$("#submit1").on('click',function () {
    if($("#username").val().trim() == '') {
        $(".tips").html('老兄！！你用户名呢！？？？');
        $("#logo").css({"width":"72px"});
        $("#logo").attr("src",'static/images/login/null-password.jpg')
    } else if($("#password").val().trim() == '') {
        $(".tips").html('老兄！！你密码呢！？？？');
        $("#logo").css({"width":"72px"});
        $("#logo").attr("src",'static/images/login/null-password.jpg')
    } else {
        $(".login").css({
            "left":"-404px",
            "opacity":"0",
        });
        $(".verBox").css({
            "left":"0",
            "opacity":"1"
        })
    }
});

var InterValObj;
var count = 60;
var curCount;
function sendMessage() {

    curCount = count;
    $("#btnSendCode").attr("disabled", true);
    $("#btnSendCode").css({"background": "#ada9a5"});
    $("#btnSendCode").val("请" + curCount + "秒后再试");
    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
    $.ajax({
        type: "POST",
        dataType: "json",
        url: '/loginCode',
        data: {phone:$("#pn").val()},
        success:function(result){
            if(result.success){
                layer.open({
                    title: false
                    ,icon:6
                    ,content: '<div style="text-align: center">验证码已发送，<br/>请您注意查收</div>'
                    , btn: []
                    ,anim: 0
                    ,shade:0
                    ,resize:false
                    ,closeBtn: 0
                    ,offset: 'rb'
                    ,time: 3500
                });
            }else{
                layer.open({
                    title: false
                    ,icon:7
                    ,content: '<div  id="laymsg" style="text-align: center">十分抱歉<br/>由于网络故障，<br/>验证码在赶来的途中迷路了</div>'
                    , btn: []
                    ,anim: 0
                    ,shade:0
                    ,resize:false
                    ,closeBtn: 0
                    ,offset: 'rb'
                    ,time: 3500
                });
                if(result.msg!="网络故障"){
                    $("#laymsg").html(result.msg)
                }
            }
        }
    });
}
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);
        $("#btnSendCode").attr('disabled',false);
        $("#btnSendCode").css({"background": "#1E90FF"});
        $("#btnSendCode").val("重新发送");
    }
    else {
        curCount--;
        $("#btnSendCode").val("请" + curCount + "秒后再试");
    }
}
$(function () {

    var regForm =$("#regform").Validform({
        tiptype:3,
        postonce:true,
        usePlugin:{
            passwordstrength:{
                minLen:6,
                maxLen:15,
                trigger:function(obj,error){
                    if(error){
                        $("#valick").show();
                        $(".passwordStrength").hide();
                    }else{
                        $("#valick").hide();
                        $(".passwordStrength").show();
                    }
                }
            }
        },
        callback:function(data){
            let serializeObject = data.serializeObject();
            $.post("/register",serializeObject,function (result) {
                if(result.success){
                    regForm.eq(0).resetForm();
                    layer.open({
                        title: '注册成功'
                        ,content: '<span>恭喜您注册成功,<br/>5s后自动跳转</span>'
                        ,closeBtn: 0
                        ,success: function(layero, index){
                            setTimeout(jump, 1000 * 5);
                            layer.close(index);
                            function jump() {
                                $("#loginbtn_regis").click();
                            }
                        }
                        ,anim:1
                        ,btn:'确认'
                        ,yes: function(index, layero){
                            $("#loginbtn_regis").click();
                            layer.close(index);
                        }
                    });
                }else{
                    regForm.eq(0).resetForm();
                    $(".passwordStrength").hide();
                    $(".regtips").html(result.msg);
                }
            });
            return false;
        }
    });

    var msgForm =$("#msgform").Validform({
        tiptype:3,
        postonce:true,
        callback:function(data){
            let serializeObject = data.serializeObject();
            $.post("/loginPhone",serializeObject,function (result) {
                if(result.success){
                    window.location.href = "/main";
                }else{
                    layer.open({
                        title: '提示'
                        ,icon:7
                        ,content: '<div id="lgmsg" style="text-align: center">网络故障</div>'
                        ,closeBtn: 0
                        ,anim:6
                        ,resize:false
                        ,scrollbar: false
                    });
                    $("#lgmsg").html(result.msg);
                    msgForm.eq(0).resetForm();
                }
            });
            return false;
        }
    });
});
function wxLogin() {

    var obj = new WxLogin({
        self_redirect:false,
        id:"login_container",
        appid: "wxd853562a0548a7d0",
        scope: "snsapi_login",
        redirect_uri: "http://bugtracker.itsource.cn/wechat/callback",
        state: "pornhub",
        style: "",
        href: ""
    });
    layer.open({
        type: 1,
        title: false,
        shade: [0.6, '#000'],
        resize:false,
        anim:4,
        content: $('#login_container')
    });
};
function faceLogin() {
    window.location.href='/face/index';
}

$("#regisbtn_login").on('click',function () {
    $(".regis").css({
        "left":"0px",
        "opacity":"1"
    });
    $(".login").css({
        "left":"-404px",
        "opacity":"0",
    });
})
$("#regisbtn_msg").on('click',function () {
    $(".regis").css({
        "left":"0px",
        "opacity":"1"
    });
    $(".msglogin").css({
        "left":"-404px",
        "opacity":"0",
    });
})
$("#loginbtn_regis").on('click',function () {
    $(".login").css({
        "left":"0px",
        "opacity":"1",
    });
    $(".regis").css({
        "left":"404px",
        "opacity":"0"
    });
})
$("#loginbtn_msg").on('click',function () {
    $(".msglogin").css({
        "left":"-404px",
        "opacity":"0",
    });
    $(".login").css({
        "left":"0px",
        "opacity":"1",
    });
})
$("#msgbtn_login").on('click',function () {
    $(".login").css({
        "left":"404px",
        "opacity":"0",
    });
    $(".msglogin").css({
        "left":"0px",
        "opacity":"1",
    });
})

$("#msgbtn_regis").on('click',function () {
    $(".regis").css({
        "left":"404px",
        "opacity":"0"
    });
    $(".msglogin").css({
        "left":"0px",
        "opacity":"1",
    });
})


